package com.chanjet.donkey.util;

import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.metamodel.source.MetadataImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;

public class HibernateEventsIntegrator implements Integrator { 
	
	@SuppressWarnings("serial")
	public void registerListeners(EventListenerRegistry registry) {
		registry.getEventListenerGroup(EventType.POST_INSERT).appendListener(new PostInsertEventListener() {
			public boolean requiresPostCommitHanding(EntityPersister persister) {
				return false;
			}
			public void onPostInsert(PostInsertEvent event) {
				System.out.print("onPostInsert====================");
				System.out.println(event.getEntity());
			}
		});
	}

	public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
		System.out.println("integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry)");
		registerListeners(serviceRegistry.getService(EventListenerRegistry.class));
	}

	public void integrate(MetadataImplementor metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
		System.out.println("integrate(MetadataImplementor metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry)");
		registerListeners(serviceRegistry.getService(EventListenerRegistry.class));
	}

	public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
		System.out.println("void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry)");
	}
	
}
